import 'package:app/common/colors.dart';
import 'package:app/common/tool.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/components/custom_footer.dart';
import 'package:app/services/address.dart';
import 'package:app/utils/hexcolor.dart';
import 'package:app/utils/search_module.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import '../../common/app_theme.dart';
import '../../components/nodata_page.dart';
import '../../router/app_pages.dart';
import 'leave_controller.dart';
import 'leave_model.dart';

class LeaveView extends GetView{


  @override
  final LeaveController controller = Get.put(LeaveController());

  LeaveView({super.key});


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      backgroundColor: HexColor('#F7F7F7'),
      appBar: BaseAppBar(
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
        action: [
          TextButton(onPressed: ()async{
           var data= await Get.toNamed(AppRoutes.createLeave);
           if(data =='refresh'){
             controller.requestData();
           }
          }, child: const Text('新建',style: TextStyle(color: Colors.white),))
        ],
        child: const Text('请假'),
      ),
      body: GetBuilder<LeaveController>(builder: (_){
        return GestureDetector(
          onPanDown: (_) {
            /// 列表滑动响应
            SearchModule.closeKeyboard(context);
          },

          onTap: (){
            SearchModule.closeKeyboard(context);
          },
          child: Column(
            children: [
              Container(
                width: Get.width,
                height: 50,
                color: Colors.white,
                child: Row(
                  children: [
                    Container(

                      margin: const EdgeInsets.only(left: 15),
                      child: Row(
                        children: [
                          GestureDetector(
                            child: Text(
                              controller.replyType,
                              style: appThemeData.textTheme.bodyText1!
                                  .copyWith(fontSize: 13),
                            ),
                            onTap: () => controller.topClick(),
                          ),
                          controller.isTopShow == false
                              ? Image.asset(
                            'images/arrow-down.png',
                            width: 12,
                            height: 12,
                          )
                              : Image.asset(
                            'images/arrow-up.png',
                            width: 12,
                            height: 12,
                            color: AppColor.themeColor,
                          )
                        ],
                      ),
                    ),
                    Container(
                        decoration: BoxDecoration(
                            color:HexColor('#F7F7F7'),
                            // color: Colors.white,
                            borderRadius: const BorderRadius.all(Radius.circular(20))
                        ),
                        width: Get.width-100,
                        alignment: Alignment.centerLeft,
                        margin: const EdgeInsets.only(top: 0,left: 15,right: 15),
                        padding: const EdgeInsets.only(left: 15),
                        height: 35,
                        child: Row(
                          children: [
                            Image.asset('images/top_search.png',width: 15,height: 15,),
                            Container(
                              alignment: Alignment.center,
                              color: Colors.transparent,
                              width: Get.width-160,
                              padding: const EdgeInsets.only(left: 5),
                              height: 35,
                              child: TextField(
                                controller: controller.editingController,
                                onChanged: (values){
                                  controller.requestData(search: values);
                                },
                                decoration: InputDecoration(
                                    isCollapsed: true,
                                    border: InputBorder.none,
                                    hintText: '请输入关键词',hintStyle: TextStyle(
                                    color: AppColor.hintColor,fontSize: 13
                                )
                                ),
                                style: TextStyle(color: AppColor.smallTextColor, fontSize: 14),
                              ),
                            )
                          ],
                        )
                    )
                  ],
                ) ,
              ),
              Expanded(child:  EasyRefresh.custom(

                controller: controller.easyRefreshController,
                  onRefresh: ()=>controller.onRefresh(),
                  onLoad: ()=> controller.onLoad(),
                  emptyWidget: controller.dataArr.isNotEmpty?null:NoDataPage(),
                  header: ClassicalHeader(),
                  // header: MaterialHeader(),
                  footer: MaterialFooter1(),
                  slivers: [
                    SliverPadding(padding: const EdgeInsets.only(top: 0),
                    sliver: SliverList(
                      delegate: _mySliverChildBuilderDelegate(),
                    ),
                    )
                  ])),
            ],
          ),
        );
      }),
    );
  }
  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
          (BuildContext context, int index) {

            LeaveItemModel model = controller.dataArr[index];
        return GestureDetector(
          onTap: () async{
            // Get.toNamed(AppRoutes.leaveDetail,arguments: model.id);
            // Get.to(CreateLeaveView());
            var data= await Get.toNamed(AppRoutes.createLeave,arguments: model);
            if(data =='refresh'){
              controller.requestData();
            }
          },
          child: Container(
            height: 120,
            padding: const EdgeInsets.only(top: 10),
            child:  Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  color: Colors.white,
                  padding: const EdgeInsets.only(top: 15,left: 15),
                  alignment: Alignment.topLeft,
                  child: Container(
                    width: 40,height: 40,
                    clipBehavior: Clip.hardEdge,
                    decoration: const BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(20))
                    ),
                    child: CachedNetworkImage(imageUrl: '${Address.homeHost}${model.image_128}',
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                Expanded(child: Container(
                  width: Get.width-10,
                  color: Colors.white,
                  padding: const EdgeInsets.all(10),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        color: Colors.white,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Container(
                              width: 150,
                              padding: const EdgeInsets.only(left: 0,right: 0),
                              child: Text('${model.userName!}提交的${model.name}',
                                style: appThemeData.textTheme.bodyText1!.
                                copyWith(fontWeight: FontWeight.w500,fontSize: 16),maxLines: 1,),
                            ),
                            Container(
                              decoration: BoxDecoration(
                                  color: HexColor('#4369F1').withAlpha(50),
                                  borderRadius: const BorderRadius.all(Radius.circular(15))
                              ),
                              padding: const EdgeInsets.only(left: 5,right: 5,top: 1,bottom: 1),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(model.state=='draft'?'待提交':
                                  model.state=='confirm'?'待批准':
                                  model.state=='validate'?'已批准':'已拒绝'
                                    ,style: appThemeData.textTheme.subtitle1?.copyWith(
                                        color: AppColor.themeColor,fontSize: 11
                                    ),),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                      const SizedBox(height: 5,),

                      Container(
                        padding: const EdgeInsets.only(left: 0,right: 5),
                        child: Text('请假类型:${model.typeName}',
                          style: appThemeData.textTheme.subtitle1!,),
                      ),
                      Container(
                        padding: const EdgeInsets.only(left: 0,right: 5,top: 5),
                        child: Column(
                          children: [
                            Text('开始时间:${model.dateStart}',style: appThemeData.textTheme.subtitle1,),
                            const SizedBox(height: 5,),
                            Text('结束时间:${model.dateEnd}',style: appThemeData.textTheme.subtitle1,),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),),
              ],
            ),
          ),
        );
      },
      childCount: controller.dataArr.length,
    );
  }

}